package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Person;
import com.itheima.edu.info.manager.service.Service;

import java.util.Scanner;

public class Controller{
    Scanner sc = new Scanner(System.in);
    String flag;
    String flag1;
    public Controller(String flag,String flag1) {
        this.flag = flag;
        this.flag1 = flag1;

    }
    Service service = new Service();

    public void strat() {
        while (true) {
            System.out.println("--------欢迎来到 <"+flag+"> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加"+flag+"  2.删除"+flag+"  3.修改"+flag+"  4.查看"+flag+"  5.退出");
            String choice = sc.nextLine();
            switch (choice) {
                case "1":
                    //添加
                    addPerson();
                    break;
                case "2":
                    //删除
                    deletePerson();
                    break;
                case "3":
                    //修改
                    updataPerson();
                    break;
                case "4":
                    //查看
                    findAllPerson();
                    break;
                case "5":
                    //退出
                    System.out.println("感谢使用"+flag+"管理系统,再见!");
                    return;
                default:
                    System.out.println("您输入有误,请重新输入");
                    break;

            }
        }

    }

    public void updataPerson() {
        System.out.println("请输入您要修改的"+flag+"ID:");
        String id;
        while (true) {
            id = sc.nextLine();
            boolean exists = service.findPerson(id);
            if (exists){
                break;
            }else{
                System.out.println("您输入的"+flag+"ID不存在,请重新输入:");
            }
        }
        System.out.println("请输入您要修改的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入您要修改的年龄:");
        String age = sc.nextLine();
        System.out.println("请输入您要修改的生日:");
        String birthday = sc.nextLine();

        Person person = new Person(id, name, age, birthday);
        service.updataPerson(id,person);
        System.out.println("修改成功!");
    }

    public void deletePerson() {
        System.out.println("请输入您要删除的"+flag+"ID:");
        String id;
        while (true) {
            id = sc.nextLine();
            boolean exists = service.findPerson(id);
            if (exists){
                service.deletePerson(id);
                System.out.println("删除成功!");
                break;
            }else{
                System.out.println("您输入的"+flag+"ID不存在,请重新输入:");
            }
        }

    }

    public void findAllPerson() {
        Person[] per = service.findAllPerson();
        System.out.println("ID\t\t姓名\t年龄\t生日");
        for (int i = 0; i < per.length; i++) {
            Person person = per[i];
            if (person != null) {
                System.out.println(person.getId() + "\t" + person.getName() + "\t" + person.getAge() + "\t\t" + person.getBirthday());
            }
        }
    }

    public void addPerson() {
        String id;
        System.out.println("请输入您要添加的"+flag+"ID:");
        while (true) {
            id = sc.nextLine();
            boolean exists = service.findPerson(id);
            if (!exists) {
                break;
            } else {
                System.out.println("你输入的"+flag+"ID已经存在,请重新输入:");
            }
        }
        System.out.println("请输入您要添加的姓名:");
        String name = sc.nextLine();
        System.out.println("请输入您要添加的年龄:");
        String age = sc.nextLine();
        System.out.println("请输入您要添加的生日:");
        String birthday = sc.nextLine();

        Person person = new Person(id, name, age, birthday);
        service.addPerson(person);
        System.out.println("添加成功!");
    }
}
